﻿Option Explicit On
Imports System.Threading

Module Sound
    Private terminate As Boolean = False
    Private sounds As New Queue(Of String)
    Private MainThread As New Thread(AddressOf threadSound)

    Public Sub initSound()
        MainThread.Start()
    End Sub

    Public Sub deinitSound()
        terminate = True
        If MainThread.ThreadState = ThreadState.Suspended Then MainThread.Resume()
    End Sub

    Public Sub playSound(ByVal name As String)
        sounds.Enqueue(name)
        If MainThread.ThreadState = ThreadState.Suspended Then MainThread.Resume()
    End Sub

    Sub threadSound()
        While Not terminate
            MainThread.Suspend()
            While sounds.Count > 0
                Dim name As String = sounds.Dequeue
                My.Computer.Audio.Play(getConfig(name), AudioPlayMode.WaitToComplete)
            End While
        End While
    End Sub

End Module
